//
//  MyinviteNotesMainController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/6/6.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//
#import "SHMyInviteNotesController.h"
#import "MyinviteNotesMainController.h"


@interface MyinviteNotesMainController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
{
    NSMutableArray             *_vcArray; //保存vc的数组
    UIPageViewController       *_pageVC;//标题滚动视图
    NSInteger                  _curPage;//指示当前页码
}


@end

@implementation MyinviteNotesMainController

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = KBASEBGCOLOR;
    
    [self showTotalInviteNum];

    NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"会员",@"代理商",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedData];
    segmentedControl.frame = CGRectMake( 0, 0,160, 30);

    //这个是设置按下按钮时的颜色
    segmentedControl.tintColor = KSHTEXTCOLOR;
    segmentedControl.selectedSegmentIndex = 0;//默认选中的按钮索引

    // 下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:KSHSystemFont(12),NSFontAttributeName,KSHTEXTCOLOR, NSForegroundColorAttributeName, nil];

    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];

    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];

    [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];

    //设置分段控件点击相应事件
    [segmentedControl addTarget:self action:@selector(segmentedControlAction:)forControlEvents:UIControlEventValueChanged];

    //将分段控件添加到导航栏上
    [[self navigationItem] setTitleView:segmentedControl];

    self.navigationController.navigationItem.titleView = segmentedControl;

    [self createSubVC];
    [self createPageVC];
}

#pragma mark - 分段选择事件

- (void)segmentedControlAction:(UISegmentedControl *)segmentedControl{
        
        NSInteger index  = segmentedControl.selectedSegmentIndex;
        //要跳转到的vc索引
        //direction：0代表前进，1代表后退
        NSInteger toPage = index ;
        [_pageVC setViewControllers:@[_vcArray[toPage]] direction:_curPage>toPage animated:NO completion:^(BOOL finished) {
            _curPage = toPage;
        }];
    
}
    
    

    


#pragma mark --- 显示总共邀请人数
- (void)showTotalInviteNum{
   
    [MyDataService requestAFURL:[NSString stringWithFormat:KGetInviters,1,500] httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        if ([result[@"success"] isEqualToNumber:@(1)] ) {
            
            NSUInteger nums = [result[@"data"] count];
            UIBarButtonItem *numBtn = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"总共%ld人",(unsigned long)nums] style:UIBarButtonItemStylePlain target:nil action:nil];
            [numBtn setWidth:-5];
            
            [numBtn setTitleTextAttributes:@{NSFontAttributeName:KSHSystemFont(14),NSForegroundColorAttributeName:KREDCOLOR} forState:UIControlStateNormal];
            self.navigationItem.rightBarButtonItem = numBtn;

            
        }
    } failure:^(id erro) {
       
    }];

    
}




#pragma mark   --------------显示邀请记录----------------
//构建数据源
- (void)createSubVC
{
    _vcArray = [[NSMutableArray alloc]init];
    SHMyInviteNotesController *memberVc = [[SHMyInviteNotesController alloc]init];
    memberVc.userIdentutyType = SHUserIsMember;
    SHMyInviteNotesController *agentVc = [[ SHMyInviteNotesController alloc]init];
    agentVc.userIdentutyType = SHUserIsAget;
    [_vcArray addObject:memberVc];
    [_vcArray addObject:agentVc];

}



- (void)createPageVC
{
    _pageVC = [[UIPageViewController alloc]initWithTransitionStyle:1 navigationOrientation:0 options:nil];
    _pageVC.delegate = self;
    _pageVC.dataSource = self;
    [_pageVC setViewControllers:@[_vcArray[0]] direction:1 animated:NO completion:nil];
    
    _pageVC.view.frame = CGRectMake(0,0, KScreenWidth, KScreenHeight );//- __I6Width(160)
    [self.view addSubview:_pageVC.view];
    
    //遍历pageVC.view的子视图，找到scrollview
    for (UIView *view in _pageVC.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            //使用kvc赋值
            //[view setValue:self forKeyPath:@"delegate"];
            ((UIScrollView *)view).delegate =self;
            ((UIScrollView *)view).pagingEnabled = YES;
        }
    }
}


#pragma mark - PageVCDelegate
//返回下一页VC
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    //拿到当前显示的vc对应在数组中的索引
    NSInteger index = [_vcArray indexOfObject:viewController];
    //如果当前显示的已经是最后一页
    if (index == _vcArray.count -1) {
        return nil;
        
    }
    
    return _vcArray[index+1];
}
//返回上一页
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [_vcArray indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return _vcArray[index-1];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    // NSLog(@"completed!");
    _curPage = [_vcArray indexOfObject:_pageVC.viewControllers[0]];
    //NSLog(@"%ld",_curPage);
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    


}


-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
}


@end
